package cn.hanyx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * Created by Administrator on 2017/2/23.
 */
public class FileSplitUtil {

    public static void splitFile(String source, String outputDir, int length) throws Exception{
        File sourceFile = new File(source);
        File outDir  = new File(outputDir);
        if (!outDir.exists()) {
            outDir.mkdir() ;
        }
        Scanner scan = new Scanner(new FileInputStream(sourceFile)) ;
        int count = 1 ;
        StringBuffer sb = new StringBuffer(128) ;
        while (scan.hasNext()) {
            sb.append(scan.next()) ;
            if (sb.length() > length) {
                String outputFileName = outputDir + File.separator + "三体" + count + ".txt" ;
                count ++ ;
                PrintStream out = new PrintStream(new FileOutputStream(outputFileName)) ;
                out.print(sb);
                out.close();
                sb = new StringBuffer(128) ;
            }
        }
        scan.close();
    }

    public static void main(String[] args) throws Exception{
        String url =FileSplitUtil.class.getResource("/").getPath() ;
        String sourcePath = new File(url).getParentFile().getParentFile().getPath() + File.separator + "src/main/resources/";
        FileSplitUtil.splitFile(sourcePath + "三体全集.txt", sourcePath + "output", 10240); // 生成文本库
        System.out.println(sourcePath);

    }
}
